/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.cco;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.cco.exception.CentroComercialException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalCentroComercialException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalCentroComercialRuntimeException;
import co.gov.medellin.sdep.sisdep.cco.exception.LocalException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalCambioException;
import co.gov.medellin.sdep.sisdep.cco.exception.SolicitudLocalException;
import co.gov.medellin.sdep.sisdep.cco.service.ICentroComercialService;
import co.gov.medellin.sdep.sisdep.cco.service.ILocalCentroComercialService;
import co.gov.medellin.sdep.sisdep.cco.service.ILocalService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.LocalCentroComercial;

/**
 * @author Usuario
 * 
 */
public class LocalCentroComercialController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/** bean para el manejo del formulario de busqueda LocalCentroComercial */
	public LocalCentroComercial localCentroComercialSearch = new LocalCentroComercial();

	/** Objeto contenedor del listado de LocalCentroComercial luego de una consulta. */
	private transient DataModel localCentroComercialListModel;

	/** bean para el manejo del formulario de creacion/edicion. LocalCentroComercial */
	private LocalCentroComercial localCentroComercial = new LocalCentroComercial();

	/** instancia de servicio localCentroComercialService */
	private ILocalCentroComercialService localCentroComercialService;

	private List listaCentrosComerciales;
		
	private List listaLocales;
	
	/** instancia de servicio centroComercialService */
	private ICentroComercialService centroComercialService;
	
	/** instancia de servicio localService */
	private ILocalService localService;
	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public LocalCentroComercialController() {
		this.localCentroComercialListModel = new ListDataModel();
		listaCentrosComerciales = new ArrayList();
		listaLocales = new ArrayList();
	}
	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchLocalCentroComercialByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getLocalCentroComercialService().load(1, 10,
					this.localCentroComercialSearch);
			this.localCentroComercialListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.localCentroComercialListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.localCentroComercialListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.LOCAL_CENTRO_COMERCIAL_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return String
	 */
	public String searchLocalCentroComercialByIdCCo() {
		String msg = "";
		try {
			List valores = null;
			localCentroComercialSearch.setIdCentroComercial(new Long(localCentroComercialSearch.getIdCentroComercialStr()));
			valores = getLocalCentroComercialService().loadByIdCCo(1, 10,
					this.localCentroComercialSearch);
			this.localCentroComercialListModel.setWrappedData(valores);
			listaCentrosComerciales = new ArrayList();
			try {
				if(getCentroComercialService().listaCentrosComerciales() != null){
					for(int i = 0; i<getCentroComercialService().listaCentrosComerciales().size();i++){			
						List listaT = new ArrayList();
						String registro[];
						listaT = (List)getCentroComercialService().listaCentrosComerciales().get(i);	
						registro = (String[])listaT.get(0);
						listaCentrosComerciales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
						listaT = null;
						registro=null;
					}
				}
			} catch (CentroComercialException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.localCentroComercialListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.localCentroComercialListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.LOCAL_CENTRO_COMERCIAL_SEARCH_VIEW;
	}
  /**
	 * Metodo que permite espeficicar la funcionalidad para la gestion [Crear / Actualizar] de un elmento tipo:
	 * LocalCentroComercial.
	 *     
	 * @author programador.java@consult-soft.com  05-06-2012
	 * @return String Vista a la cual debe retornar
	 * 	 
	 */
	public String manage() {
		String returnView = AppConstants.LOCAL_CENTRO_COMERCIAL_SEARCH_VIEW;
		if(this.localCentroComercial.getId() == null) {
       returnView = create();
		} else {
       returnView = update();
		}
		return returnView;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.localCentroComercial.getId() == null) {
				localCentroComercial.setIdCentroComercial(new Long(localCentroComercial.getIdCentroComercialStr()));
				localCentroComercial.setIdLocal(new Long(localCentroComercial.getIdLocalStr()));
				getLocalCentroComercialService().insert(this.localCentroComercial);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (LocalCentroComercialException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (LocalCentroComercialRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- localCentroComercial " + msg, null, null);
		return AppConstants.LOCAL_CENTRO_COMERCIAL_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.localCentroComercial.getId() != null) {
				localCentroComercial.setIdCentroComercial(new Long(localCentroComercial.getIdCentroComercialStr()));
				localCentroComercial.setIdLocal(new Long(localCentroComercial.getIdLocalStr()));
				getLocalCentroComercialService().update(this.localCentroComercial);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (LocalCentroComercialException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (LocalCentroComercialRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.LOCAL_CENTRO_COMERCIAL_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createLocalCentroComercial() {
		localCentroComercial = new LocalCentroComercial();
		localCentroComercial.setId(null);
		localCentroComercial.setUsuarioCrea(getLoggedUserID());
		localCentroComercial.setUsuarioModifica(getLoggedUserID());
		localCentroComercial.setIdCentroComercialStr(localCentroComercialSearch.getIdCentroComercialStr());
		//localCentroComercial.setEstado("ACTIVO");
		listaCentrosComerciales = new ArrayList();
		listaLocales = new ArrayList();
		try {
			if(getCentroComercialService().listaCentrosComerciales() != null){
				for(int i = 0; i<getCentroComercialService().listaCentrosComerciales().size();i++){			
					List listaT = new ArrayList();
					String registro[];
					listaT = (List)getCentroComercialService().listaCentrosComerciales().get(i);	
					registro = (String[])listaT.get(0);
					listaCentrosComerciales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
					listaT = null;
					registro=null;
				}
			}
			if(getLocalService().listaLocales() != null){
				for(int i = 0; i<getLocalService().listaLocales().size();i++){			
					List listaT = new ArrayList();
					String registro[];
					listaT = (List)getLocalService().listaLocales().get(i);	
					registro = (String[])listaT.get(0);
					listaLocales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
					listaT = null;
					registro=null;
				}
			}
		} catch (CentroComercialException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LocalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return AppConstants.LOCAL_CENTRO_COMERCIAL_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateLocalCentroComercial() {
		Map params = getExternalContext().getRequestParameterMap();
		String localCentroComercialDataIndex = (String) params
				.get("localCentroComercialDataIndex");
		this.localCentroComercial = (LocalCentroComercial) ((List) this.localCentroComercialListModel
				.getWrappedData())
				.get(Integer.parseInt(localCentroComercialDataIndex));
		
		listaCentrosComerciales = new ArrayList();
		listaLocales = new ArrayList();
		try {
			if(getCentroComercialService().listaCentrosComerciales() != null){
				for(int i = 0; i<getCentroComercialService().listaCentrosComerciales().size();i++){			
					List listaT = new ArrayList();
					String registro[];
					listaT = (List)getCentroComercialService().listaCentrosComerciales().get(i);	
					registro = (String[])listaT.get(0);
					listaCentrosComerciales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
					listaT = null;
					registro=null;
				}
			}
			if(getLocalService().listaLocales() != null){
				for(int i = 0; i<getLocalService().listaLocales().size();i++){			
					List listaT = new ArrayList();
					String registro[];
					listaT = (List)getLocalService().listaLocales().get(i);	
					registro = (String[])listaT.get(0);
					listaLocales.add(new SelectItem(registro[0].toString(),registro[1].toString()));
					listaT = null;
					registro=null;
				}
			}
		} catch (CentroComercialException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LocalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return AppConstants.LOCAL_CENTRO_COMERCIAL_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String cancelLocalCentroComercial() {
		return AppConstants.LOCAL_CENTRO_COMERCIAL_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public LocalCentroComercial getLocalCentroComercialSearch() {
		return localCentroComercialSearch;
	}

	/**
	 * 
	 * @param localCentroComercialSearch
	 */
	public void setlocalCentroComercialSearch(LocalCentroComercial localCentroComercialSearch) {
		this.localCentroComercialSearch = localCentroComercialSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getlocalCentroComercialListModel() {
		return localCentroComercialListModel;
	}

	/**
	 * 
	 * @param localCentroComercialListModel
	 */
	public void setlocalCentroComercialListModel(DataModel localCentroComercialListModel) {
		this.localCentroComercialListModel = localCentroComercialListModel;
	}

	/**
	 * 
	 * @return
	 */
	public LocalCentroComercial getLocalCentroComercial() {
		return localCentroComercial;
	}

	/**
	 * 
	 * @param localCentroComercial
	 */
	public void setlocalCentroComercial(LocalCentroComercial localCentroComercial) {
		this.localCentroComercial = localCentroComercial;
	}

	/**
	 * obtencion del servicio localCentroComercialService
	 * 
	 * @return
	 */
	public ILocalCentroComercialService getLocalCentroComercialService() {
		return localCentroComercialService;
	}

	/**
	 * establecimiento del servicio localCentroComercialService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param localCentroComercialService
	 */
	public void setLocalCentroComercialService(
			ILocalCentroComercialService localCentroComercialService) {
		this.localCentroComercialService = localCentroComercialService;
	}

	public ICentroComercialService getCentroComercialService() {
		return centroComercialService;
	}

	public void setCentroComercialService(
			ICentroComercialService centroComercialService) {
		this.centroComercialService = centroComercialService;
	}

	public ILocalService getLocalService() {
		return localService;
	}

	public void setLocalService(ILocalService localService) {
		this.localService = localService;
	}

	public List getListaLocales() {
		return listaLocales;
	}

	public void setListaLocales(List listaLocales) {
		this.listaLocales = listaLocales;
	}

	public List getListaCentrosComerciales() {
		return listaCentrosComerciales;
	}

	public void setListaCentrosComerciales(List listaCentrosComerciales) {
		this.listaCentrosComerciales = listaCentrosComerciales;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
